﻿using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class NoteSh : NoteContent
    {
        private ushort _col;
        private bool _fColHidden;
        private bool _fRwHidden;
        private bool _fShow;
        private ushort _idObj;
        private ushort _row;
        private XLUnicodeString _stAuthor;
        private byte unused2;

        public NoteSh(Cell cell, BiffWriterWorkbookContext context)
        {
            Contract.Requires(cell != null);
            Contract.Requires(cell.Comment != null);
            _col = (ushort) cell.Column.Index;
            _row = (ushort) cell.Row.Index;
            _fShow = cell.Comment.IsVisible;
            _fRwHidden = !cell.Row.IsVisible;
            _fColHidden = !cell.Column.IsVisible;
            IdObj = context.GetObjIdFromComment(cell.Comment);
            this._stAuthor = (XLUnicodeString) cell.Comment.Author;
        }

        public NoteSh()
        {
        }

        public ushort Column
        {
            get { return _col; }
            set { _col = value; }
        }

        public ushort Row
        {
            get { return _row; }
            set { _row = value; }
        }

        public ushort IdObj
        {
            get { return _idObj; }
            set { _idObj = value; }
        }

        public bool IsVisible
        {
            get { return _fShow; }
        }

        public bool FRwHidden
        {
            get { return _fRwHidden; }
            set { _fRwHidden = value; }
        }

        public bool FColHidden
        {
            get { return _fColHidden; }
            set { _fColHidden = value; }
        }

        public string Author
        {
            get { return (string)_stAuthor; }
        }

        /* public XLUnicodeString Author
        {
            get { return _stAuthor; }
            set  { _stAuthor = value; }
        }*/

        public override void Read(BinaryReader reader)
        {
#if DEBUG
            ReadWriteChecker.CheckClass((t, r) => t.Read(r),
                                        (t, w) => t.Write(w), reader, this);
#endif
            Row = reader.ReadUInt16();
            Column = reader.ReadUInt16();
            ushort ag = reader.ReadUInt16();
            _fShow = (ag & 0x02) != 0;
            FRwHidden = (ag & 0x80) != 0;
            FColHidden = (ag & 0x100) != 0;
            Contract.Assert(Ag == ag);
            IdObj = reader.ReadUInt16();
           //\\ Debug.WriteLine("Reading " + IdObj);
            _stAuthor = new XLUnicodeString();
            _stAuthor.Read(reader);
            this.unused2 = reader.ReadByte();
        }
        ushort Ag
        {
            get
            {
                ushort result = 0;
                if (IsVisible) result |= 0x02;
                if (FRwHidden) result |= 0x80;
                if (FColHidden) result |= 0x100;
                return result;
            }
        }
        public override void Write(BinaryWriter writer)
        {
            writer.Write(Row);
            writer.Write(Column);
            writer.Write(Ag);
            writer.Write(IdObj);
            _stAuthor.Write(writer);
            writer.Write(unused2);
        }
    }
}